/**
 * 按钮组件
 */
class SimpleButton extends egret.DisplayObjectContainer {

	public constructor() {
		super();
		mouse.setButtonMode(this, true);
		this.touchEnabled=true;
	}

	private skin:egret.MovieClip;
	//从资源创建按钮皮肤
	public createSkinByName(file:string,name:string){
		let skin=this.skin=Tools.createMovieClipByName(file,name);
		this.addChild(skin);
		skin.gotoAndStop(1);
	}
	//从影片剪辑创建按钮皮肤
	public createMovieClipSkin(mc:egret.MovieClip){
		let skin=this.skin=mc;
		this.addChild(skin);
		skin.gotoAndStop(1);
		//mouse.setButtonMode(skin, true);
	}
	//
	public enable(value:Boolean){
		//console.log("####");
		if(value){
			this.addEventListener(mouse.MouseEvent.MOUSE_OVER,this.onMouseOver,this);
			this.addEventListener(mouse.MouseEvent.MOUSE_OUT,this.onMouseOut,this);
			this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onMouseDown,this);
			this.addEventListener(egret.TouchEvent.TOUCH_END,this.onMouseUp,this);
		}else {
			this.removeEventListener(mouse.MouseEvent.MOUSE_OVER,this.onMouseOver,this);
			this.removeEventListener(mouse.MouseEvent.MOUSE_OUT,this.onMouseOut,this);
			this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onMouseDown,this);
			this.removeEventListener(egret.TouchEvent.TOUCH_END,this.onMouseUp,this);
			let skin=this.skin;
			skin.gotoAndStop(1);
		}
	}
	//鼠标悬停在按钮上
	public onMouseOver(e:egret.TouchEvent){
		let skin=this.skin;
		skin.gotoAndStop(2);
		//console.log("mouse over " + e.target.name + "  " + e.bubbles);
	}
	//鼠标点下按钮
	public onMouseDown(e:egret.TouchEvent){
		let skin=this.skin;
		skin.gotoAndStop(3);
	}
	//鼠标离开
	public onMouseOut(e:egret.TouchEvent){
		let skin=this.skin;
		skin.gotoAndStop(1);
	}
	//鼠标弹起
	public onMouseUp(){
		let skin=this.skin;
		skin.gotoAndStop(2);
	}
}